﻿using BAL;
using DAL;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Module.QLHoaDon
{
    public partial class frmThemChiTietHoaDon : Form
    {
        int _mode;
        HoaDon hdOBJ;
        ChiTietHoaDon ctObj;
        public frmThemChiTietHoaDon()
        {
            InitializeComponent();
            _mode = 1;
        }

        public frmThemChiTietHoaDon(string maHDMoi)
        {
            InitializeComponent();
            hdOBJ = new HoaDon();
            hdOBJ.maHoaDon = maHDMoi;
            _mode = 1;
        }

        public frmThemChiTietHoaDon(ChiTietHoaDon ctHD)
        {
            InitializeComponent();
            ctObj = ctHD;
            txtMaHang.Text = ctHD.maHang;
            txtSoluong.Text = ctHD.soLuongMua.ToString();
            _mode = 2;
        }

        ChiTietHoaDonBAO ctBAO = new ChiTietHoaDonBAO();
        KhachHangBAO khBAO = new KhachHangBAO();
        NhanVienBAO nvBAO = new NhanVienBAO();
        HoaDonDAO hdDAO = new HoaDonDAO();
        HangHoaBAO hhBAO = new HangHoaBAO();

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (hhBAO.SelectNameHangHoa(txtMaHang.Text).Rows.Count == 0)
                    throw new Exception("Khong co ma hang nao !");

                DataTable dt;
                if (_mode == 1)
                    dt = ctBAO.SelectAllChiTietHoaDon(hdOBJ.maHoaDon);
                else
                    dt = ctBAO.SelectAllChiTietHoaDon(ctObj.maHoaDon);
                DataView dv = new DataView(dt);
                dv.RowFilter = String.Format("maHang like '{0}'", txtMaHang.Text);

                if (dv.Count == 1 && _mode == 1)
                    throw new Exception("Da ton tai ma hang trong hoa don");
                txtTenHang.Text = hhBAO.SelectNameHangHoa(txtMaHang.Text).Rows[0]["TenHang"].ToString();

                try
                {
                    if (Convert.ToInt32(txtSoluong.Text) <= 0)
                        throw new Exception("So luong phai lon hon hoac bang 0");
                    else
                    {
                        HangHoa hhObj = hhBAO.GetHangHoaObj(txtMaHang.Text);
                        if (Convert.ToInt32(txtSoluong.Text) > hhObj.soLuong)
                        {
                            hhObj.soLuong = 0;
                            txtSoluong.Text = hhObj.soLuong.ToString();
                        }
                        else
                        {
                            hhObj.soLuong -= Convert.ToInt32(txtSoluong.Text);
                        }


                        hhObj.maHang = txtMaHang.Text;
                        hhBAO.HangHoa_Update(hhObj);
                        ChiTietHoaDon ctHD = new ChiTietHoaDon();
                        //DataTable dtHangHoa=hhBAO.SelectNameHangHoa(hhObj.maHang);
                        ctHD.giaTruocVAT = hhObj.giaGoc;
                        if (_mode == 1)
                            ctHD.maHoaDon = hdOBJ.maHoaDon.ToUpper();
                        else
                            ctHD.maHoaDon = ctObj.maHoaDon.ToUpper();
                        ctHD.maGiamGiaHang = "";
                        ctHD.maHang = hhObj.maHang;
                        ctHD.VAT = hhObj.VAT;
                        ctHD.soLuongMua = Convert.ToInt32(txtSoluong.Text);

                        if (_mode == 1)
                            ctBAO.InsertChiTietHoaDon(ctHD);
                        if (_mode == 2)
                            ctBAO.UpdateChiTietHoaDon(ctHD);
                            //Console.WriteLine("update");
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    txtSoluong.Focus();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                txtMaHang.Focus();
            }
        }


        private void txtMaHang_TextChanged(object sender, EventArgs e)
        {
            if (hhBAO.SelectNameHangHoa(txtMaHang.Text).Rows.Count != 0)
                txtTenHang.Text = hhBAO.SelectNameHangHoa(txtMaHang.Text).Rows[0]["TenHang"].ToString();
            else
                txtTenHang.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmThemHoaDon_Load(object sender, EventArgs e)
        {

        }



        //public List<> 
    }
}
